package com.yang.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yang.pojo.CompanionDto;
import com.yang.service.CheckInService;
import com.yang.util.PublicUtil;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class CheckInController {
    private static Logger logger = Logger.getLogger(CheckInController.class);

    @Resource
    CheckInService checkInService;

    /**
     * 查询价格
     * @param roomNum
     * @param perDay
     * @return
     */
    @RequestMapping(produces = "application/json;charset=UTF-8",value = {"admin/selectMoney","customer/selectMoney"},method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> selectMoney(String roomNum,String perDay){
        Map<String,Object> map = new HashMap<String, Object>();
        if(roomNum == null || perDay == null || roomNum.equals("") || perDay.equals("")){
            map.put("status","error");
            map.put("info","输入存在空");
            return map;

        }
        map = checkInService.selectMoneyService(roomNum,perDay);
        return map;
    }

    /**
     * 提交入住信息
     * @param request
     * @param jsonString
     * @return
     */
    @RequestMapping(produces = "application/json;charset=UTF-8",value = {"admin/submitInformation","customer/submitInformation"},method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> submitInformation(HttpServletRequest request,String jsonString ){
        Map<String,Object> map = new HashMap<String, Object>();
        List<CompanionDto> companionDtos = new ArrayList<CompanionDto>();
        logger.info("jsonString = "+jsonString);
        JSONObject json = JSON.parseObject(jsonString);
        String pay = json.getString("pay");
        logger.info("pay = "+pay);
        if(pay == null || pay.equals("")){
            map.put("status","error");
            map.put("info","请付款！");
            return map;
        }
        //获取房主
        String frist = json.getString("frist");
        JSONObject fristObject = JSON.parseObject(frist);
        CompanionDto companionDto = new CompanionDto(fristObject.getString("guestName"),fristObject.getString("identity"),
                fristObject.getString("tel"),fristObject.getString("perDay"),fristObject.getString("roomNum"),
                fristObject.getString("sex"));
        String date = fristObject.getString("beginDay");
        long beginTime;
        if(date == null || date.equals("") ){
            beginTime = System.currentTimeMillis();
        }else{
            date = date.replaceAll("-","");
            logger.info("date = "+date);
            beginTime = PublicUtil.chargeDatToMillis(date);
        }
        logger.info("companiondto = "+companionDto.toString());

        //第一个为租房提交着
        companionDtos.add(companionDto);

        //其他同伴
        String companion = json.getString("companionList");
        JSONArray companionList = JSONArray.parseArray(companion);

        for(Object companionObj :companionList){
            CompanionDto comp = new CompanionDto();
            comp.setGuestName(JSONObject.parseObject(JSONObject.toJSONString(companionObj)).getString("guestName"));
            comp.setSex(JSONObject.parseObject(JSONObject.toJSONString(companionObj)).getString("sex"));
            comp.setIdentity(JSONObject.parseObject(JSONObject.toJSONString(companionObj)).getString("identity"));
            comp.setTel(JSONObject.parseObject(JSONObject.toJSONString(companionObj)).getString("tel"));
            logger.info("comp = "+comp.toString());
            companionDtos.add(comp);
        }
        for (CompanionDto companionDto1 : companionDtos){
            logger.info(companionDto1.toString());
        }

        map = checkInService.resideservice(companionDtos,pay,beginTime, request);
        return map;
    }
}
